package com.ilxqx.framework.system.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.ilxqx.framework.system.entity.anno.Excel;
import com.ilxqx.framework.system.entity.anno.FileKey;
import com.ilxqx.framework.system.enumeration.SysUserStatusEnum;
import com.ilxqx.framework.system.serializer.FileKeyFieldSerializer;
import com.ilxqx.framework.system.serializer.SysRoleSerializer;
import lombok.Data;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * 用户实体类
 * @author venus
 */
@Entity(name = "sys_user")
@Data
@DynamicInsert
@DynamicUpdate
public class SysUser implements Serializable {

    private static final long serialVersionUID = 3347653058853817482L;

    /**
     * 用户主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    /**
     * 用户账号
     */
    @Excel("账号")
    private String account;

    /**
     * 用户密码
     */
    @JsonIgnore
    private String password;

    /**
     * 用户姓名
     */
    @Excel("姓名")
    private String name;

    /**
     * 用户头像
     */
    @FileKey
    @JsonSerialize(using = FileKeyFieldSerializer.class)
    @Excel("头像")
    private String avatar;

    /**
     * 联系电话
     */
    @Excel("手机")
    private String phone;

    /**
     * 联系邮箱
     */
    @Excel("邮箱")
    private String email;

    /**
     * 用户备注
     */
    @Excel("备注")
    private String remark;

    /**
     * 用户状态
     */
    @Excel(value = "状态", method = "getMeaning")
    private SysUserStatusEnum status;

    /**
     * 操作用户
     */
    private Long operationUserId;

    /**
     * 操作用户姓名
     */
    @Excel("操作用户")
    private String operationUserName;

    /**
     * 创建时间
     */
    @Excel("创建时间")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Excel("更新时间")
    private LocalDateTime updateTime;

    /**
     * 用户角色
     */
    @JsonSerialize(contentUsing = SysRoleSerializer.class)
    @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
    @JoinTable(name = "SysUserRole",
            joinColumns = @JoinColumn(name = "userId"),
            inverseJoinColumns = @JoinColumn(name = "roleId"))
    private Set<SysRole> userRoles;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        SysUser sysUser = (SysUser) o;

        return new EqualsBuilder()
                .append(userId, sysUser.userId)
                .append(account, sysUser.account)
                .append(password, sysUser.password)
                .append(name, sysUser.name)
                .append(avatar, sysUser.avatar)
                .append(phone, sysUser.phone)
                .append(email, sysUser.email)
                .append(remark, sysUser.remark)
                .append(status, sysUser.status)
                .append(operationUserId, sysUser.operationUserId)
                .append(operationUserName, sysUser.operationUserName)
                .append(createTime, sysUser.createTime)
                .append(updateTime, sysUser.updateTime)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(userId)
                .append(account)
                .append(password)
                .append(name)
                .append(avatar)
                .append(phone)
                .append(email)
                .append(remark)
                .append(status)
                .append(operationUserId)
                .append(operationUserName)
                .append(createTime)
                .append(updateTime)
                .toHashCode();
    }

}
